﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using Bussiness_Objects;

namespace DataObjects.ADO
{
    public class SqlDauSach : IDauSachDao
    {
        public DataTable GetTable()
        {
            string sqlString = "sp_DAUSACH_selectAll";
            return Db.Read(sqlString, null);
        }

        public DataTable GetById(object businessData)
        {
            DauSach _sach = businessData as DauSach;

            if (_sach != null)
            {
                _sach.SetParameterSelectID();
                string sqlString = "sp_DAUSACH_selectByID";
                return Db.Read(sqlString, _sach.parms);
            }
            return null;
        }

        public void GetMaDauSach(object businessData)
        {
            DauSach _sach = businessData as DauSach;

            if (_sach != null)
            {
                _sach.parms = new object[2];
                SqlParameter par = new SqlParameter("@TenDauSach", _sach.TenDauSach);
                _sach.parms[0] = par;

                par = new SqlParameter();
                par.ParameterName = "@MaDauSach";
                par.SqlDbType = SqlDbType.Int;
                par.Direction = ParameterDirection.Output;                
                _sach.parms[1] = par;

                Db.ExecuteStoreProcedure("sp_DAUSACH_selectMaDauSach", _sach.parms);
                if (((SqlParameter)_sach.parms[1]).Value.ToString() != "")
                    _sach.MaDauSach = int.Parse(((SqlParameter)_sach.parms[1]).Value.ToString());
                else
                    _sach.MaDauSach = 0;
            }
        }

        public string Insert(object businessData)
        {
            DauSach _sach = businessData as DauSach;

            if (_sach != null)
            {
                _sach.SetParameterInsert();
                string sqlString = "sp_DAUSACH_insert";
                string result = Db.ExecuteStoreProcedure(sqlString, _sach.parms);
                _sach.MaDauSach = (int)((SqlParameter)_sach.parms[2]).Value;
            }
            return "Error";
        }

        public string Update(object businessData)
        {
            DauSach _sach = businessData as DauSach;

            if (_sach != null)
            {
                _sach.SetParameterUpdate();
                string sqlString = "sp_DAUSACH_update";
                return Db.ExecuteStoreProcedure(sqlString, _sach.parms);
            }
            return "Error";
        }

        public string Delete(object businessData)
        {
            DauSach _sach = businessData as DauSach;
            if (_sach != null)
            {
                _sach.SetParameterDelete();
                string sqlString = "sp_DAUSACH_delete";
                return Db.ExecuteStoreProcedure(sqlString, _sach.parms);
            }
            return "Error";
        }
    }
}
